package atencion_de_emergencia.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import examenes.bean.OrdenExamenesDTO;
import atencion_de_emergencia.bean.OaeDTO;
import atencion_de_emergencia.bean.OaeHcDTO;
import atencion_de_emergencia.bean.ResponsablePacienteDTO;
import atencion_de_emergencia.interfaces.OaeDAO;
import utilitarios.MiConexion;

public class MySqlOaeDAO implements OaeDAO{

	@Override
	public String registrarOAE(OaeDTO objOAE,ResponsablePacienteDTO objResPac) throws SQLException {
		// TODO Auto-generated method stub
		Connection conexion = MiConexion.obtenerConexion();
		CallableStatement call = null;
		String strCodigo = null;
		
			call = conexion.prepareCall("{call usp_registraOAE(?,?,?,?,?,?,?,?,?,?)}");
			call.registerOutParameter(10, java.sql.Types.CHAR);

		call.setString(1,objOAE.getStrNroHC());
		call.setString(2,objOAE.getStrCodAsis());
		call.setString(3,objOAE.getUsuario());
		call.setString(4,objOAE.getTip_ope());
		
		call.setString(5,objResPac.getStrNom_Res());
		call.setString(6,objResPac.getStrApe_Pat_Res());
		call.setString(7,objResPac.getStrApe_Mat_Res());
		call.setString(8,objResPac.getStrNum_Doc());
		call.setString(9,objResPac.getStrCod_Tipdocu());
		
		call.executeUpdate();
		strCodigo = call.getString(10);
		conexion.close();
		
		return strCodigo;
	}

	@Override
	public List<OaeHcDTO> listadoOAE(String strEstado) {
		// TODO Auto-generated method stub
		List<OaeHcDTO> oaes = new ArrayList<OaeHcDTO>();

		Connection cn = MiConexion.obtenerConexion();
		try {

			String sql	=	"select oae.num_oae,oae.fecha_oae,oae.num_hc,hc.nom_pac,hc.ape_pat_pac,hc.ape_mat_pac " +
					        "from orden_de__atencion_de__emergencia oae inner join " +
					        "historia_clinica hc on oae.num_hc=hc.num_hc where estado_oae=?";

			PreparedStatement st = cn.prepareStatement(sql);
			st.setString(1, strEstado);
			ResultSet rs = st.executeQuery();

			while (rs.next()) {
						
				OaeHcDTO objOaeHc = new OaeHcDTO();
				
				objOaeHc.setStrNroOAE(rs.getString(1));
				objOaeHc.setDtFecha(rs.getDate(2));
				objOaeHc.setDtHora(rs.getTime(2));	
				objOaeHc.setNum_Hc(rs.getString(3));
				objOaeHc.setNom_Pac(rs.getString(4));
				objOaeHc.setApe_Pat_Pac(rs.getString(5));
				objOaeHc.setApe_Mat_Pac(rs.getString(6));
				oaes.add(objOaeHc);
				
			}
			st.close();
			rs.close();
			cn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return oaes;
	}

	@Override
	public void actualizarOAE(OrdenExamenesDTO oe, String strEstado)
			throws SQLException {
		// TODO Auto-generated method stub
		Connection cn = MiConexion.obtenerConexion();
		String sql	= "update orden_de__atencion_de__emergencia set estado_oae=? where num_oae=?";
		PreparedStatement st = cn.prepareStatement(sql);
		st.setString(1, strEstado);
		st.setString(2, oe.getStrNum_Oae());
		st.executeUpdate();
		
		st.close();
		cn.close();
	}

	@Override
	public void actualizarOAE(OaeHcDTO objOaeHc, String string) throws SQLException {
		// TODO Auto-generated method stub
		Connection cn = MiConexion.obtenerConexion();
		String sql	= "update orden_de__atencion_de__emergencia set estado_oae=? where num_oae=?";
		PreparedStatement st = cn.prepareStatement(sql);
		st.setString(1, string);
		st.setString(2, objOaeHc.getStrNroOAE());
		st.executeUpdate();
		
		st.close();
		cn.close();
	}

}
